package com.haima.utils;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.haima.common.core.redis.RedisCache;
import com.haima.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Component
public class WxTool {
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private RedisCache redisCacheService;
    private static ISysConfigService sysConfigService;
    private static RedisCache redisCache;
    private static String ACCESS_TOKEN_KEY = "accessToken";
    private static String loginUrl = "https://api.weixin.qq.com/sns/jscode2session";
    private static String atUrl = "https://api.weixin.qq.com/cgi-bin/token";
    private static String phoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
    private static String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
    private static String shortLinkUrl = "https://api.weixin.qq.com/wxa/genwxashortlink";

    @PostConstruct
    public void init() {
        sysConfigService = this.configService;
        redisCache = this.redisCacheService;
    }

    public static String getLoginInfo(String code) {
        String appId = sysConfigService.selectConfigByKey("wx.appid");
        String appSecret = sysConfigService.selectConfigByKey("wx.appsecret");
        String params = "appid=" + appId + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=authorization_code";
        String sr = HttpRequest.sendGet(loginUrl, params);
        JSONObject json = JSONObject.parseObject(sr);
        String openid = (String)json.get("openid");
        return openid;
    }

    public static String getPhoneNum(String code) {
        String access_token = getAccessToken();
        JSONObject json = new JSONObject();
        json.put("code", code);
        String jsonstr = JSONObject.toJSONString(json, new JSONWriter.Feature[0]);
        String ret = HttpRequest.sendPost(phoneUrl + "?access_token=" + access_token, jsonstr);
        JSONObject res = JSONObject.parseObject(ret);
        Integer errcode = res.getInteger("errcode");
        if (errcode == 0) {
            JSONObject phoneinfo = res.getJSONObject("phone_info");
            String phone = phoneinfo.getString("phoneNumber");
            return phone;
        } else {
            return null;
        }
    }

    public static String getAccessToken() {
        String appId = sysConfigService.selectConfigByKey("wx.appid");
        String appSecret = sysConfigService.selectConfigByKey("wx.appsecret");
        String at = "";
        if (redisCache.hasKey("wechat_token:" + ACCESS_TOKEN_KEY)) {
            at = (String)redisCache.getCacheObject("wechat_token:" + ACCESS_TOKEN_KEY);
        } else {
            String params = "appid=" + appId + "&secret=" + appSecret + "&grant_type=client_credential";
            String sr = HttpRequest.sendGet(atUrl, params);
            JSONObject json = JSONObject.parseObject(sr);
            at = json.getString("access_token");
            redisCache.setCacheObject("wechat_token:" + ACCESS_TOKEN_KEY, at, 110, TimeUnit.MINUTES);
        }

        return at;
    }

    public static String getShortLink(String page,String title) {
        String access_token = getAccessToken();
        JSONObject json = new JSONObject();
        json.put("page_url", page);
        json.put("page_title", title);
        String jsonstr = JSONObject.toJSONString(json, new JSONWriter.Feature[0]);
        String ret = HttpRequest.sendPost(shortLinkUrl + "?access_token=" + access_token, jsonstr);
        JSONObject res = JSONObject.parseObject(ret);
        Integer errcode = res.getInteger("errcode");
        if (errcode == 0) {
            String link = res.getString("link");
            return link;
        } else {
            return null;
        }
    }
}
